package example.handler.impl;

import example.constant.PreConditionsCheck;
import example.constant.ResponseEnum;
import example.exception.BizException;
import example.handler.AdminUserHandler;
import example.model.AdminUser;
import example.param.AdminUserDeleteParam;
import example.param.AdminUserEditParam;
import example.param.AdminUserLoginParam;
import example.param.AdminUserSignInParam;
import example.service.AdminUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by snksnk1091 on 2017/8/23.
 */
@Component
@SessionAttributes("currentUser")
public class AdminUserHandlerImpl implements AdminUserHandler {
    @Autowired
    private AdminUserService adminUserService;

    public void adminUserSignIn(AdminUserSignInParam param){
        PreConditionsCheck.checkArgument(adminUserService.countAdminUserByName(param.getUserName()) > 0 , ResponseEnum.USERNAME_ERROR);
        AdminUser adminUser = new AdminUser();
        adminUser.setUserName(param.getUserName());
        adminUser.setPassword(param.getPassword());
        adminUser.setPower(param.getPower());
        adminUser.setIsDelete(false);
        adminUser.setCreatDate(new Date());
        adminUser.setUpdateDate(new Date());
        adminUserService.adminUserSignInByAdminUser(adminUser);
    }

    public void adminUserEdit(AdminUserEditParam param){
        PreConditionsCheck.checkArgument(adminUserService.countAdminUserById(param.getId()) <= 0 , ResponseEnum.CHECK_VOID);
        PreConditionsCheck.checkArgument(adminUserService.countAdminUserByName(param.getUserName()) > 0 , ResponseEnum.USERNAME_ERROR);
        AdminUser adminUser = adminUserService.getAdminUserById(param.getId());
        adminUser.setUserName(param.getUserName());
        adminUser.setPassword(param.getPassword());
        adminUser.setPower(param.getPower());
        adminUserService.updateAdminUser(adminUser, param.getId());
    }

    public void adminUserDelete(AdminUserDeleteParam param){
        adminUserService.deleteAdminUserByUserName(param.getUserName());
    }

    public AdminUser getAdminUser(int id) {
        PreConditionsCheck.checkArgument(adminUserService.countAdminUserById(id) <= 0 , ResponseEnum.CHECK_VOID);
        return adminUserService.getAdminUserById(id);
    }

    public Object adminUserLogin(AdminUserLoginParam param, HttpSession httpSession) {
        AdminUser currentUser = (AdminUser) httpSession.getAttribute("currentUser");
        if(currentUser==null || !currentUser.getUserName().equals(param.getUserName())){
            PreConditionsCheck.checkArgument(adminUserService.countAdminUserByName(param.getUserName()) <= 0, ResponseEnum.CHECK_VOID);
            AdminUser adminUser = adminUserService.getAdminUserByUserName(param.getUserName());
            if(param.getPassword().equals(adminUser.getPassword())){
                httpSession.setAttribute("currentUser", adminUser);
                return ResponseEnum.SUCCESS;
            }
            return ResponseEnum.PASSWORDCHECK_ERROR;
        }

        return ResponseEnum.SUCCESS;
    }
}
